﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using Tong;
public partial class aspnet_admin_bock_News_CX_News_ShowTree : System.Web.UI.Page
{
    TongUse TextData = new TongUse();
    protected void Page_Load(object sender, EventArgs e)
    {
        Tong.WorkXml.OperateXmlByDataSet.WriteXml(ShowXml(), Server.MapPath("~/aspnet/admin/Menu/Menu_Data/TreeView_DataSource.xml"));
    
    }
    #region 显示树的XML文件 调用方法 ShowTree()

    /// <summary>
    /// SelectXML 方法要输出的XML文本文件 使用请
    /// </summary>
    string XmlText = "<?xml version='1.0' encoding='utf-8'?>\r<系统菜单>";


    /// <summary>
    /// 写XML层次函数
    /// </summary>
    /// <param name="parentId">参数parentId 递归从属 ID</param>
    /// <param name="layer">参数parentId 递归 层次ID 相当于XML文件的层次结构 </param>
    protected void SelectXML(int parentId, int layer)
    {
        int i_n = 0, j, cId = 0;
        string sNbsp = "";//输出的空格 \t 为TAB 

        //建立记录集  相当于ASP 中的rs 

        DataTable Myds =TextData.SqlataSet("Select * From CX_Column where CX_Column_LX<>3 and CX_Column_followid =" + parentId + " order by CX_Column_id desc") .Tables[0];//调用存贮过程创建DataTalbe对象

        j = Myds.Rows.Count;// Myds.Rows.Count记录集中的记录数

        for (int i = 0; i <= (layer - 1); i++)//输入TAB 显示层次
        {
            sNbsp += "\t \t";
        }

        //XmlText += "<一级分类菜单 ";//输出XML



        for (int i = 0; i < j; i++)
        {

            cId = Int32.Parse(Myds.Rows[i]["CX_Column_id"].ToString());//得到现在行的索引值;相当于ASP rs("id")

            DataTable MyDt = TextData.SqlataSet("Select * From CX_Column where CX_Column_id="+ cId).Tables[0];//建立从属记录集

            if (MyDt.Rows.Count > 0)//看看有是不是不只一条记录
            {
               
                    //下面方法给URL编码了
                    // XmlText += sNbsp + cId + "<一级分类菜单 分类菜单名='" + MyDt.Rows[0]["ClassName"].ToString() + "' 显示url='~/" + TextData.CodeUrlText(MyDt.Rows[0]["URL"].ToString()) + "' Trang='main'>1\n";
                    XmlText += sNbsp + cId + "<一级分类菜单 分类菜单名='" + MyDt.Rows[0]["CX_Column_Name"].ToString() + ((ISNewsColumn(cId.ToString())) ? Server.HtmlEncode("<span style=\"color:#ff0000;\">新闻</span>") : "") + "' 显示url='~/" + MyDt.Rows[0]["CX_Column_ItemTemplate_Url"].ToString().Replace("&", "&amp;") + "' Event=\"" + outputEvtstr(cId.ToString()) + "\"  Trang='main'>1\n";
               
            }
            else//如果只有一条记录
            {

                    //下面方法给URL编码了
                    //XmlText += sNbsp + cId + "<一级分类菜单 分类菜单名='" + Myds.Rows[0]["ClassName"].ToString() + "' 显示url='~/" + TextData.CodeUrlText(Myds.Rows[0]["URL"].ToString()) + "' Trang='main'>2\n";
                    XmlText += sNbsp + cId + "<一级分类菜单 分类菜单名='" + MyDt.Rows[0]["CX_Column_Name"].ToString() +((ISNewsColumn(cId.ToString()))?Server.HtmlEncode("<span style=\"color:#ff0000;\">新闻</span>"):"") + "' 显示url='~/" + Myds.Rows[0]["CX_Column_ItemTemplate_Url"].ToString().Replace("&", "&amp;") + "' Event=\"" + outputEvtstr(cId.ToString()) + "\" Trang='main'>2\n";
           

            }
            SelectXML(cId, (layer + 1));//调用函数本身执行递归


                XmlText += sNbsp + "</一级分类菜单>\n";
            

        }
        // XmlText += "</一级分类菜单>\n"; 一定要删掉　不然ＸＭＬ文件无法显示
    }

    /// <summary>
    /// 返回XML字符
    /// </summary>
    public String ShowXml()
    {
        SelectXML(0, 0);
        XmlText += "</系统菜单>";
        return XmlText;
    }
    #endregion

    /// <summary>
    /// 输出STR
    /// </summary>
    /// <param name="Cid"></param>
    /// <returns></returns>
    protected string outputEvtstr(string Cid)
    {

        if (ISNewsColumn(Cid))
        {
            return "javascript:cu('" + Cid + "')";
        }
        else
        {
            return "javascript:";
        }

    }

    /// <summary>
    /// 看看这个传入的ID是不是新闻栏目
    /// </summary>
    /// <param name="Cid"></param>
    /// <returns></returns>
    protected bool ISNewsColumn(string Cid)
    {
        DataTable Tmptab = TextData.SqlataSet("select  CX_Column_LX from [CX_Column] where CX_Column_id=" + Cid + "").Tables[0];
        try
        {
            if (Tmptab.Rows[0][0].ToString() == "1")
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        catch
        {
            return false;
        }
    }



}
